package com.wenhuitec.sysui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
//import com.gzwh.weblib.bean.Result;
//import com.gzwh.weblib.utils.MyObserver;
//import com.gzwh.weblib.utils.RequestUtils;
import com.wenhuitec.sysui.R;
import com.wenhuitec.sysui.adapter.ShowKucun2Adapter;
import com.wenhuitec.sysui.entity.FoodSkuModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 在添加菜品时底部库存弹出框
 */
public class ShowKuCun2Dialog {
    private View view;

    private Button butSave;

    private RecyclerView stockRv;

    private TextView foodNameTv;

    private Context mContext;
    private List<FoodSkuModel> mFoodSkuModelList;

    private ShowKucun2Adapter showFoodSkuAdapter;

    public void BottomDialog(Context context, String foodName, List<FoodSkuModel> foodSkuModelList) {
        //1、使用Dialog、设置style
        final NUDialog dialog = new NUDialog(context, R.style.DialogTheme);
        //2、设置布局
        view = View.inflate(context, R.layout.dialog_kc2, null);
        this.mFoodSkuModelList = foodSkuModelList;
        stockRv = view.findViewById(R.id.stockRv);
        butSave = view.findViewById(R.id.but_save);
        foodNameTv = view.findViewById(R.id.foodNameTv);

        butSave.setOnClickListener(v -> {
            //向服务器存储数据
            this.mFoodSkuModelList = showFoodSkuAdapter.getFoodSkuModelList();

            List<Object> reqList = new ArrayList<>();
            for (FoodSkuModel goo : mFoodSkuModelList) {
                Map<String, String> map = new HashMap<>();
                map.put("skuId", goo.getId());
                map.put("stock", String.valueOf(goo.getStock()));
                reqList.add(map);
            }
//            RequestUtils.updateStockBath(context, JSONArray.toJSONString(reqList), new MyObserver<String>(mContext) {
//                @Override
//                public void onSuccess(Result<String> result) {
//                    dialog.dismiss();
//                }
//
//                @Override
//                public void onFailure(Throwable e, String errorMsg) {
//
//                }
//            });

        });
        mContext = context;
        dialog.setContentView(view);

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        foodNameTv.setText(foodName);


        showFoodSkuAdapter = new ShowKucun2Adapter(mContext, mFoodSkuModelList);
        stockRv.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));//设置布局管理器
        stockRv.setAdapter(showFoodSkuAdapter);
    }

    public class NUDialog extends Dialog {
        private Context mContext;

        public NUDialog(@NonNull Context context, int themeResId) {
            super(context, themeResId);
            mContext = context;
        }

        @Override
        public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
            //如果是点击事件，获取点击的view，并判断是否要收起键盘
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                //获取目前得到焦点的view
                View v = getCurrentFocus();
                //判断是否要收起并进行处理
                if (isShouldHideKeyboard(v, ev)) {
                    hideKeyboard(v.getWindowToken());
                    v.clearFocus();
                }
            }
            //这个是activity的事件分发，一定要有，不然就不会有任何的点击事件了
            return super.dispatchTouchEvent(ev);

        }
    }

    //判断是否要收起键盘
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        //如果目前得到焦点的这个view是editText的话进行判断点击的位置
        if (v instanceof EditText) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            // 点击EditText的事件，忽略它。
            return !(event.getX() > left) || !(event.getX() < right)
                    || !(event.getY() > top) || !(event.getY() < bottom);
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上
        return false;
    }

    //隐藏软键盘并让editText失去焦点
    private void hideKeyboard(IBinder token) {
        if (token != null) {
            //这里先获取InputMethodManager再调用他的方法来关闭软键盘
            //InputMethodManager就是一个管理窗口输入的manager

            InputMethodManager im = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (im != null) {
                im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }
}